<?php

/*============================================
Classe Iaswitchconf
============================================*/

class Iaswitchconf extends ModelObject{

	protected static $cached_object_list= array();
	protected $LIB_CONF;
	protected $DEVICE_NAME;
	protected $SWITCH_NAME;
	protected $SWITCH_POS;
	protected $SWITCH_ADR;

	protected function __construct( $LIB_CONF,$DEVICE_NAME,$SWITCH_NAME,$SWITCH_POS,$SWITCH_ADR){
		try{
			
			$this->LIB_CONF = $LIB_CONF;
			$this->DEVICE_NAME = $DEVICE_NAME;
			$this->SWITCH_NAME = $SWITCH_NAME;
			$this->SWITCH_POS = $SWITCH_POS;
			$this->SWITCH_ADR = $SWITCH_ADR;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getLIBCONF(){
		return $this->LIB_CONF;
	}

	public function getDEVICENAME(){
		return DEVICENAME::get($this->DEVICE_NAME);
	}

	public function getSWITCHNAME(){
		return $this->SWITCH_NAME;
	}

	public function getSWITCHPOS(){
		return $this->SWITCH_POS;
	}

	public function getSWITCHADR(){
		return $this->SWITCH_ADR;
	}

	public function setDEVICENAME( $DEVICE_NAME){
		try{
			$temp = $this->DEVICE_NAME;
			if(is_null($DEVICE_NAME)){
				throw new Exception("Le paramètre \$dEVICE_NAME de type get_class($dEVICE_NAME) est null alors que la colonne dans la base est marquée comme NOT NULL");
			}
			else if(!($DEVICE_NAME instanceof DEVICENAME) || is_null($DEVICE_NAME->getId())){
				throw new Exception("Le paramètre \$dEVICE_NAME de type get_class($dEVICE_NAME) n'est pas du bon type ou n'est pas présent dans la base");
			}
			else {
				if($this->DEVICE_NAME !== $DEVICE_NAME->getId()){
					$this->DEVICE_NAME = $DEVICE_NAME->getId();
					$this->update();
				}
			}
		}
		catch(Exception $e){
			$this->DEVICE_NAME = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setDEVICENAME() a échouée",0,$e);
		}
	}

	public function setSWITCHNAME( $SWITCH_NAME){
		try{
			$temp = $this->SWITCH_NAME;
			if($this->SWITCH_NAME !== $SWITCH_NAME){
				$this->SWITCH_NAME = $SWITCH_NAME;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SWITCH_NAME = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSWITCHNAME() a échouée",0,$e);
		}
	}

	public function setSWITCHPOS( $SWITCH_POS){
		try{
			$temp = $this->SWITCH_POS;
			if($this->SWITCH_POS !== $SWITCH_POS){
				$this->SWITCH_POS = $SWITCH_POS;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SWITCH_POS = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSWITCHPOS() a échouée",0,$e);
		}
	}

	public function setSWITCHADR( $SWITCH_ADR){
		try{
			$temp = $this->SWITCH_ADR;
			if($this->SWITCH_ADR !== $SWITCH_ADR){
				$this->SWITCH_ADR = $SWITCH_ADR;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SWITCH_ADR = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSWITCHADR() a échouée",0,$e);
		}
	}

	public static function create( $LIB_CONF,$DEVICE_NAME,$SWITCH_NAME,$SWITCH_POS,$SWITCH_ADR){
		try{
			$iaswitchconf = new Iaswitchconf($LIB_CONF, $DEVICE_NAME, $SWITCH_NAME, $SWITCH_POS, $SWITCH_ADR);
			$iaswitchconf->insert();
			Iaswitchconf::cacheObject($iaswitchconf);
			return $iaswitchconf;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type Iaswitchconf a échouée",0,$e);
		}
	}

	public static function get( $LIB_CONF){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($LIB_CONF) || $LIB_CONF === "") {
				return null;
			}
			if(Iaswitchconf::existsInCache($LIB_CONF)) {
				return Iaswitchconf::getFromCache($LIB_CONF);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `iaswitchconf` WHERE `LIB_CONF` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$LIB_CONF));
			if ($row = $stmt->fetch()){
				$iaswitchconf = new Iaswitchconf( $row['LIB_CONF'],$row['DEVICE_NAME'],$row['SWITCH_NAME'],$row['SWITCH_POS'],$row['SWITCH_ADR']);
				$iaswitchconf->id =  $row['LIB_CONF'];
				$dbh->commit();
				Iaswitchconf::cacheObject($iaswitchconf);
				return $iaswitchconf;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $LIB_CONF . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `iaswitchconf` SET `DEVICE_NAME` = :DEVICE_NAME, `SWITCH_NAME` = :SWITCH_NAME, `SWITCH_POS` = :SWITCH_POS, `SWITCH_ADR` = :SWITCH_ADR WHERE `LIB_CONF` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':DEVICE_NAME'=>$this->DEVICE_NAME, ':SWITCH_NAME'=>$this->SWITCH_NAME, ':SWITCH_POS'=>$this->SWITCH_POS, ':SWITCH_ADR'=>$this->SWITCH_ADR, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `iaswitchconf` WHERE `LIB_CONF` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			Iaswitchconf::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `iaswitchconf` ( `DEVICE_NAME`, `SWITCH_NAME`, `SWITCH_POS`, `SWITCH_ADR`) VALUES (:DEVICE_NAME, :SWITCH_NAME, :SWITCH_POS, :SWITCH_ADR)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':DEVICE_NAME'=>$this->DEVICE_NAME, ':SWITCH_NAME'=>$this->SWITCH_NAME, ':SWITCH_POS'=>$this->SWITCH_POS, ':SWITCH_ADR'=>$this->SWITCH_ADR));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `iaswitchconf`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(Iaswitchconf::existsInCache($row['LIB_CONF'])){
					$obj = Iaswitchconf::getFromCache($row['LIB_CONF']);
				}
				else{
					$obj = new Iaswitchconf( $row['LIB_CONF'],$row['DEVICE_NAME'],$row['SWITCH_NAME'],$row['SWITCH_POS'],$row['SWITCH_ADR']);
				}
				if(!is_null($obj)){
					$obj->id = $row['LIB_CONF'];
					Iaswitchconf::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iaswitchconf a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `iaswitchconf`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type Iaswitchconf a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `iaswitchconf`';
			$sql_select = 'SELECT LIB_CONF FROM `iaswitchconf`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = Iaswitchconf::getObjectFromCache($row['LIB_CONF']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						Iaswitchconf::removeObjectFromCache($row['LIB_CONF']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type Iaswitchconf a échouée",0,$e);
		}
	}

}